package com.luoyx.hauyne.tracelog.controller;

import com.luoyx.hauyne.mybatisplus.dto.PageResult;
import com.luoyx.hauyne.tracelog.api.OperationLogAPI;
import com.luoyx.hauyne.tracelog.api.request.OperationLogDTO;
import com.luoyx.hauyne.tracelog.query.OperationLogQuery;
import com.luoyx.hauyne.tracelog.response.OperationLogPageResultVO;
import com.luoyx.hauyne.tracelog.service.OperationLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "OperationLogController", description = "操作日志")
@RestController
@RequestMapping("/operation-logs")
@RequiredArgsConstructor
public class OperationLogController implements OperationLogAPI {

    private final OperationLogService operationLogService;

    /**
     * 创建操作日志
     *
     * @param operationLogDTO 操作日志DTO
     */
    @ResponseStatus(HttpStatus.CREATED)
    @Operation(summary = "创建操作日志")
    @PostMapping
    @Override
    public void create(@RequestBody OperationLogDTO operationLogDTO) {
        operationLogService.create(operationLogDTO);
    }

    @Operation(summary = "分页查询操作日志")
    @GetMapping
    public PageResult<OperationLogPageResultVO> findPage(@Validated OperationLogQuery query) {
        PageResult<OperationLogPageResultVO> page = operationLogService.findPage(query);
        List<OperationLogPageResultVO> rows = page.getRows();
        for (OperationLogPageResultVO row : rows) {
            row.setActions(row.getAction().split("；"));
        }
        page.setRows(rows);

        return page;
    }


}
